package sharetime.tag.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import sharetime.core.EntityObject;

@SuppressWarnings("unchecked")
@Entity
@Table(name = "tag", uniqueConstraints = { @UniqueConstraint(columnNames = { "tag_name" }) })
@NamedQueries( { @NamedQuery(name = "tag.findAllTagNames", query = "SELECT t.name FROM Tag AS t") })
public class Tag extends EntityObject {

	private static final long serialVersionUID = 8773079267773914597L;

	private List<TaggedEntityObject> taggedEntities = new ArrayList<TaggedEntityObject>();

	private String name;

	public Tag() {
	}

	public Tag(String name) {
		this.name = name;
	}

	@Column(name = "tag_name")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@ManyToMany(mappedBy = "tags", 
				cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, 
				fetch = FetchType.LAZY)
	public List<TaggedEntityObject> getTaggedEntities() {
		return taggedEntities;
	}

	public void setTaggedEntities(List<TaggedEntityObject> taggedEntities) {
		this.taggedEntities = taggedEntities;
	}

	@Override
	public String toString() {
		return name;
	}
}
